package com.bolt.common.ognl;


import ognl.Ognl;
import ognl.OgnlException;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * Created by Administrator on 2017/8/10.
 */
public class OgnlCache {
    private static final Map<String, Object> expressionCache = new ConcurrentHashMap<String, Object>();

    private OgnlCache() {
        // Prevent Instantiation of Static Class
    }

    public static Object getValue(String expression, Object root) throws OgnlException {
            Map<Object, OgnlClassResolver> context = Ognl.createDefaultContext(root, new DefaultMemberAccess(false), new OgnlClassResolver(),null);
            return Ognl.getValue(parseExpression(expression), context, root);
    }

    private static Object parseExpression(String expression) throws OgnlException {
        Object node = expressionCache.get(expression);
        if (node == null) {
            node = Ognl.parseExpression(expression);
            expressionCache.put(expression, node);
        }
        return node;
    }
}
